﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects.DataClasses;
using System.Linq.Expressions;
using Cherry.Agile.Data.Metadata;
using Cherry.Agile.Data.Object.Metadata;

namespace Cherry.Agile.Data.Metadata
{
        
    internal sealed class DeleteExpressionParser<T>
        :DmlExpressionParser<T>
    where T:class
    {

        #region Members

        IExpressionParser whereExpressionParser;

        #endregion

        #region Constructor

        
        public DeleteExpressionParser(string contianerName, Expression<Func<T, bool>> whereExpression)
            : base(contianerName)
        {
            whereExpressionParser = new BinaryExpressionParser<T>();
            whereExpressionParser.Expression = whereExpression;
        }

        #endregion

        #region DmlExpressionParser Overrides
        
        public override string GetDmlCommand()
        {
            StringBuilder deleteCommand = new StringBuilder();
            deleteCommand.Append(" DELETE ")
            .Append(" FROM  ")
            .Append(MetadataAccessor.GetTableNameByEdmType<T>(containerName).TableName)
            .Append(" ")
            .Append(whereExpressionParser.ParseExpression(containerName));

            return deleteCommand.ToString();
        }

        #endregion
    }
}
